package cn.guozeyiblog.client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import cn.guozeyiblog.netutils.NetWorkStatus;

public class FeedBack extends Activity {
	private EditText theme;
	private EditText content;
	private Button button;
	private ProgressBar bar;
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
           switch (msg.what) {
		case 200:
			Toast.makeText(getApplicationContext(), "谢谢你的反馈", 0).show();
			break;
		case 404:
			Toast.makeText(getApplicationContext(), "对不起,反馈失败了,你可以稍后再试试", 0).show();
			break;

		default:
			break;
		}
           theme.setText("");
           content.setText("");
           bar.setVisibility(View.GONE);
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedback);
		theme = (EditText) findViewById(R.id.feedback_editText1);
		content = (EditText) findViewById(R.id.feedback_editText2);
		button = (Button) findViewById(R.id.feedback_button1);
		bar = (ProgressBar) findViewById(R.id.feed_back_progressBar);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String stheme = theme.getText().toString().trim();
				String scontent = content.getText().toString().trim();
				if (!stheme.equals("")&&!scontent.equals("")) {
                    String feedurl = getResources().getString(R.string.feed_back_url);
                    if(NetWorkStatus.isNetworkAvailable(getApplicationContext())){
                    	new PostData().execute(feedurl,stheme,scontent);
                    	bar.setVisibility(View.VISIBLE);
                    } //network avaliable
                    else {
						Toast.makeText(getApplicationContext(), "你的网络不可用哦", 0).show();
					}
					
				}else{
					Toast.makeText(getApplicationContext(), "亲,你的支持是我最大的动力,填完可好", 0).show();
				}
			}
		});
	}

	public class PostData extends AsyncTask<String, Integer, Void>{

		@Override
		protected Void doInBackground(String... params) {
			// TODO Auto-generated method stub
			String urlString = params[0];
			HttpPost post = new HttpPost(urlString);
			List<NameValuePair> pair = new ArrayList<NameValuePair>();
			pair.add(new BasicNameValuePair("theme", params[1]));
			pair.add(new BasicNameValuePair("content", params[2]));
			System.out.println("Theme:"+params[1]+"--Content:"+params[2]);
			HttpResponse httpResponse = null;
			try {
				// 设置httpPost请求参数
				post.setEntity(new UrlEncodedFormEntity(pair, HTTP.UTF_8));
				httpResponse = new DefaultHttpClient().execute(post);
				//System.out.println(httpResponse.getStatusLine().getStatusCode());
				if (httpResponse.getStatusLine().getStatusCode() == 200) {
					// 第三步，使用getEntity方法活得返回结果
					String result = EntityUtils.toString(httpResponse.getEntity()).trim();
					System.out.println("-------------->feedbackCode:"+result);
					if (Integer.parseInt(result)==200) {
						publishProgress(200);
					}else {
						publishProgress(404);
					}
				}
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
     
		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			if (values[0]==200) {
				handler.sendEmptyMessage(200);
			}else {
				handler.sendEmptyMessage(404);
			}
			
			super.onProgressUpdate(values);
		}
	}
}
